package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import bean.UserBean;
import utils.JdbcUtil;

public class UserDao {
	
	// 注册
	public static void register(String user_name, String user_realname, String user_password, String user_phone){
		Connection conn = null;
		PreparedStatement pstmt = null;
		
		conn = JdbcUtil.conn();
		String sql = "insert into user_info values ('" +user_name + "','" + user_realname + "','" + user_password +"','" + user_phone +"');";
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JdbcUtil.relase(pstmt, conn);
		}
		
	}
	
	// 检测手机号是否被注册
	public static boolean check_phone(String phone) {
		boolean flag = false;
		
		Connection conn = JdbcUtil.conn();
		PreparedStatement pstmt	= null;
		ResultSet rSet = null;
		String sql = "select * from user_info where user_phone = '" + phone +"';";
		try {
			pstmt	= conn.prepareStatement(sql);
			rSet = pstmt.executeQuery();
			
			while(rSet.next()) {
				flag = true;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JdbcUtil.relase(rSet, pstmt, conn);
		}
		
		return flag;
	}

	// 登录检测
	public static UserBean login(String user_phone, String user_password) {
		// TODO Auto-generated method stub
		UserBean user= null;
		Connection conn = JdbcUtil.conn();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		String sql = "select * from user_info where user_phone=? and user_password=?";
		
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, user_phone);
			pstmt.setString(2, user_password);
			rs = pstmt.executeQuery();
			
			while(rs.next()) {
				user = new UserBean(rs.getString("user_name"), rs.getString("user_realname"), rs.getString("user_password"), rs.getString("user_phone"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JdbcUtil.relase(rs, pstmt, conn);
		}
		
		
		return user;
	}

}
